package com.jzh.service.impl;


import com.jzh.dao.MemberDao;
import com.jzh.pojo.Member;
import com.jzh.service.MemberService;
import com.jzh.utils.MD5Utils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 会员服务
 */
@Service(version = "1.0.0")
@Transactional
public class MemberServiceImpl implements MemberService{

    @Resource
    private MemberDao memberDao;

    @Override
    public void add(Member member) {
        if (member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //根据月份统计会员数量
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) throws ParseException {
        List<Integer> list = new ArrayList<>();
        for (String month : months) {
            //获取每月的天数
            SimpleDateFormat format = new SimpleDateFormat("yyyy.MM");
            Date dd = format.parse(month);
            Calendar cal = Calendar.getInstance();
            cal.setTime(dd);
            int cc=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            String day2 = "." + cc;
            //format:2018.03.31
            month = month + day2;
            Integer count = memberDao.findMemberCountBeforeDate(month);
            list.add(count);
        }
        return list;
    }
}
